package com.xuzimian.globaldemo.quartz.util;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;


public class SchedulerHelper {

    private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();

    /**
     * @param jobName          任务名
     * @param jobGroupName     任务组名
     * @param triggerName      触发器名
     * @param triggerGroupName 触发器组名
     * @param jobClass         任务
     * @param cron             时间设置，参考quartz说明文档
     * @Description: 添加一个定时任务
     */
    public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String cron) throws SchedulerException {

        Scheduler sched = schedulerFactory.getScheduler();
        // 任务名，任务组，任务执行类
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();

        // 触发器
        TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
        // 触发器名,触发器组
        triggerBuilder.withIdentity(triggerName, triggerGroupName);
        triggerBuilder.startNow();
        // 触发器时间设定
        triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
        // 创建Trigger对象
        CronTrigger trigger = (CronTrigger) triggerBuilder.build();

        // 调度容器设置JobDetail和Trigger
        sched.scheduleJob(jobDetail, trigger);
    }

    /**
     * @param name      任务名
     * @param groupName 任务组名
     * @param jobClass  任务
     * @param cron      时间设置，参考quartz说明文档
     * @Description: 添加一个定时任务
     */
    public static void addJob(String name, String groupName, Class jobClass, String cron) throws SchedulerException {
        addJob(name, groupName, name, groupName, jobClass, cron);
    }

    /**
     * @param jobName          任务名
     * @param jobGroupName     任务组名
     * @param triggerName      触发器名
     * @param triggerGroupName 触发器组名
     * @param jobClass         任务
     * @param cron             时间设置，参考quartz说明文档
     * @Description: 添加一个定时任务, 并启动
     */
    public static void addJobAndStart(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String cron) throws SchedulerException {
        Scheduler sched = schedulerFactory.getScheduler();
        addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron);
        // 启动
        if (!sched.isShutdown()) {
            sched.start();
        }
    }


    /**
     * @param name      任务名
     * @param groupName 任务组名
     * @param jobClass  任务
     * @param cron      时间设置，参考quartz说明文档
     * @Description: 添加一个定时任务, 并启动
     */
    public static void addJobAndStart(String name, String groupName, Class jobClass, String cron) throws SchedulerException {
        addJobAndStart(name, groupName, name, groupName, jobClass, cron);
    }


    /**
     * @param jobName
     * @param jobGroupName
     * @param triggerName      触发器名
     * @param triggerGroupName 触发器组名
     * @param cron             时间设置，参考quartz说明文档
     * @Description: 修改一个任务的触发时间
     */
    public static void modifyJobTimeForCronTime(String jobName, String jobGroupName, String triggerName, String triggerGroupName, String cron) throws SchedulerException {

        Scheduler sched = schedulerFactory.getScheduler();
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
        CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
        if (trigger == null) {
            return;
        }

        String oldTime = trigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(cron)) {
            // 触发器
            TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
            // 触发器名,触发器组
            triggerBuilder.withIdentity(triggerName, triggerGroupName);
            triggerBuilder.startNow();
            // 触发器时间设定
            triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
            // 创建Trigger对象
            trigger = (CronTrigger) triggerBuilder.build();
            //修改一个任务的触发时间
            sched.rescheduleJob(triggerKey, trigger);

        }
    }

    /**
     * @param jobName
     * @param jobGroupName
     * @param triggerName      触发器名
     * @param triggerGroupName 触发器组名
     * @param cron             时间设置，参考quartz说明文档
     * @Description: 修改一个任务, 先删除再创建新的
     */
    public static void modifyJobTimeByNew(String jobName, String jobGroupName, String triggerName, String triggerGroupName, String cron) throws SchedulerException {
        Scheduler sched = schedulerFactory.getScheduler();
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
        CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
        if (trigger == null) {
            return;
        }

        String oldTime = trigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(cron)) {
            // 触发器
            TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
            // 触发器名,触发器组
            triggerBuilder.withIdentity(triggerName, triggerGroupName);
            triggerBuilder.startNow();
            // 触发器时间设定
            triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
            // 创建Trigger对象
            trigger = (CronTrigger) triggerBuilder.build();

            /** 先删除，然后在创建一个新的Job  */
            JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName, jobGroupName));
            Class<? extends Job> jobClass = jobDetail.getJobClass();
            removeJob(jobName, jobGroupName, triggerName, triggerGroupName);
            addJobAndStart(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron);
        }
    }

    /**
     *
     * @param name
     * @param groupName
     * @param cron
     * @throws SchedulerException
     * @Description: 修改一个任务的触发时间
     */
    public static void modifyJobTimeForCronTime(String name, String groupName, String cron) throws SchedulerException {
        modifyJobTimeForCronTime(name,groupName,name,groupName,cron);
    }

    /**
     *
     * @param name
     * @param groupName
     * @param cron
     * @throws SchedulerException
     * @Description: 修改一个任务, 先删除再创建新的
     */
    public static void modifyJobTimeByNew(String name, String groupName, String cron) throws SchedulerException {
        modifyJobTimeByNew(name,groupName,name,groupName,cron);
    }

    /**
     * @param jobName
     * @param jobGroupName
     * @param triggerName
     * @param triggerGroupName
     * @Description: 移除一个任务
     */
    public static void removeJob(String jobName, String jobGroupName,
                                 String triggerName, String triggerGroupName) throws SchedulerException {

        Scheduler sched = schedulerFactory.getScheduler();

        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);

        sched.pauseTrigger(triggerKey);// 停止触发器
        sched.unscheduleJob(triggerKey);// 移除触发器
        sched.deleteJob(JobKey.jobKey(jobName, jobGroupName));// 删除任务
    }

    /**
     * @param name
     * @param groupName
     * @Description: 移除一个任务
     */
    public static void removeJob(String name, String groupName) throws SchedulerException {
        removeJob(name,groupName,name,groupName);
    }

    /**
     * @Description:启动所有定时任务
     */
    public static void startJobs() throws SchedulerException {
        Scheduler sched = schedulerFactory.getScheduler();
        sched.start();
    }

    /**
     * @Description:关闭所有定时任务
     */
    public static void shutdownJobs() throws SchedulerException {
          Scheduler sched = schedulerFactory.getScheduler();
            if (!sched.isShutdown()) {
                sched.shutdown();
            }
    }
}
